package com.itcast.donkey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.itcast.donkey.R;
import com.itcast.donkey.modles.MapModles;
import com.itcast.donkey.modles.UriModles;
import com.tencent.mapsdk.raster.model.BitmapDescriptorFactory;
import com.tencent.mapsdk.raster.model.LatLng;
import com.tencent.mapsdk.raster.model.Marker;
import com.tencent.mapsdk.raster.model.MarkerOptions;
import com.tencent.tencentmap.mapsdk.map.MapActivity;
import com.tencent.tencentmap.mapsdk.map.MapView;
import com.tencent.tencentmap.mapsdk.map.TencentMap;
import com.tencent.tencentmap.mapsdk.map.UiSettings;

import org.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/6.
 */
public class ToolMapActivity extends MapActivity implements View.OnClickListener {

    private LinearLayout linScenicSpot;
    private ImageView backScenicApot;
    private ImageView ivChanScenicSpot;
    private TextView tvTitleScenicSpot;
    private MapView mapActivityToolMap;

    private List<MapModles> modlesList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_map);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        linScenicSpot = (LinearLayout) findViewById(R.id.lin_scenic_spot);
        backScenicApot = (ImageView) findViewById(R.id.back_scenic_apot);
        ivChanScenicSpot = (ImageView) findViewById(R.id.iv_chan_scenic_spot);
        tvTitleScenicSpot = (TextView) findViewById(R.id.tv_title_scenic_spot);
        mapActivityToolMap = (MapView) findViewById(R.id.map_activity_tool_map);
    }


    private void initData() {

        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 0);
        final String url = UriModles.TOOLBOX_FRONT_MAP_URL + id + UriModles.TOOLBOX_END_MAP_URL;
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String values = response.body().string();
                        Gson gson = new Gson();
                        try {
                            JSONArray array = new JSONArray(values);
                            for (int i = 0; i < array.length(); i++) {
                                MapModles modles = gson.fromJson(array.get(i).toString(), MapModles.class);
                                modlesList.add(modles);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (modlesList.size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }

                    }
                });
            }
        }).start();
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                    break;
                case 1:
                    double lat = Double.valueOf(modlesList.get(0).getLat());
                    double lng = Double.valueOf(modlesList.get(0).getLng());

                    //获取TencentMap实例
                    TencentMap tencentMap = mapActivityToolMap.getMap();
                    //设置卫星底图
                    tencentMap.setSatelliteEnabled(false);
                    //设置实时路况开启
                    tencentMap.setTrafficEnabled(true);
                    //设置底图中心点
                    tencentMap.setCenter(new LatLng(lat, lng));
                    //设置缩放级别
                    tencentMap.setZoom(11);

                    //获取UiSettings实例
                    UiSettings uiSettings = mapActivityToolMap.getUiSettings();
                    //设置logo到屏幕底部中心
                    uiSettings.setLogoPosition(UiSettings.LOGO_POSITION_CENTER_BOTTOM);
                    //设置比例尺到屏幕右下角
                    //uiSettings.setScaleViewPosition(UiSettings.SCALEVIEW_POSITION_RIGHT_BOTTOM);
                    //启用缩放手势(更多的手势控制请参考开发手册)
                    uiSettings.setZoomGesturesEnabled(true);
                    uiSettings.setScaleControlsEnabled(true);
                    for (int i = 0; i < modlesList.size(); i++) {
                        double lat1 = Double.valueOf(modlesList.get(i).getLat());
                        double lng1 = Double.valueOf(modlesList.get(i).getLng());
                        Marker marker = mapActivityToolMap.addMarker(new MarkerOptions()
                                .position(new LatLng(lat1, lng1))
                                .title(modlesList.get(i).getName())
                                .icon(BitmapDescriptorFactory.defaultMarker()).draggable(true));
                        //marker.showInfoWindow();//显示上面的标题
                    }

                    break;
                case 2:
                    break;
            }
        }
    };

    private void initEvent() {
        backScenicApot.setOnClickListener(this);
        ivChanScenicSpot.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_scenic_apot:
            case R.id.iv_chan_scenic_spot:
                finish();
                break;
        }
    }
}
